package de.tum.team26.eistpoll.server;

import java.util.LinkedList;


import javax.jdo.PersistenceManager;
import javax.jdo.annotations.IdGeneratorStrategy;
import javax.jdo.annotations.PersistenceCapable;
import javax.jdo.annotations.Persistent;
import javax.jdo.annotations.PrimaryKey;
import javax.jdo.annotations.Transactional;

import de.tum.team26.eistpoll.shared.PollData;


/**
* Poll Data Storage Class (Persistant)
 */
@PersistenceCapable
public class PollDS implements Cloneable{
	//Persistant Attributes
	@PrimaryKey
    @Persistent(valueStrategy = IdGeneratorStrategy.IDENTITY)
    private Long id;
	@Persistent
	private int [] optionResults;
	@Persistent
	private LinkedList<String> like;
	@Persistent
	private LinkedList<String> dislike;
	@Persistent
	private LinkedList<String> ip;
	@Persistent
	private int numVotes;
	@Persistent
	private boolean isActive;
	@Persistent
	String title;
	
	/**
	 * Constructor
	 */
	public PollDS(){
		optionResults = new int[5];
		like = new LinkedList<String>();
		dislike = new LinkedList<String>();
		ip = new LinkedList<String>();
		numVotes=0;
		isActive=false;
		title = "";
	}
	
	/**
	 * Add a Result
	 * @param option Result option
	 * @param like Like String
	 * @param dislike Dislike String
	 */
	public void addResult(int option, String like, String dislike){
		optionResults[option]++;
		if ((like != null) && (!like.equals(""))) this.like.add(like);
		if ((dislike != null) && (!dislike.equals(""))) this.dislike.add(dislike);
		numVotes++;
	}
	// Getter and Setter
	public int getNumVotes(){
		return this.numVotes;
	}
	
	public int[] getOption(){
		return optionResults;
	}
	
	public String[] getLike(){
		String[] result = new String[like.size()];
		return like.toArray(result);
	}
	
	public String[]  getDisLike(){
		String[] result = new String[dislike.size()];
		return dislike.toArray(result);
	}
	
	public Long getId(){
		return id;
	}
	
	public int getIdInt(){
		return id.intValue();
	}
	
	public void setTitle(String title){
		this.title = title;
	}
	
	public String getTitle(){
		return title;
	}
	
	/**
	 * Get Persistant Data
	 */
	@SuppressWarnings("unchecked")
	public void getPersistantData(){
		PersistenceManager pm = PMF.get().getPersistenceManager();
		try{
			PollDS e = pm.getObjectById(PollDS.class, id);
			this.optionResults = e.optionResults.clone();
			this.like = (LinkedList<String>) e.like.clone();
			this.dislike = (LinkedList<String>) e.dislike.clone(); 
			this.ip = (LinkedList<String>) e.ip.clone();
			this.numVotes = e.numVotes;
			this.isActive = e.isActive;
		} finally{
			pm.close();
		}
	}

	/**
	 * Set persistant Data
	 */
	public void setPersistantData(){
		PersistenceManager pm = PMF.get().getPersistenceManager();
        try{
        	pm.makePersistent(this);
        } finally{
        	pm.close();
        }

	}
	
	/**
	 * Get Data of a Poll for transfer to the client
	 * @return PollData Object
	 */
	@SuppressWarnings("unchecked")
	public PollData getPollData(){
		PollData temp = new PollData();
		temp.setId(getIdInt());
		temp.optionResults = this.optionResults.clone();
		temp.like = (LinkedList<String>) this.like.clone();
		temp.dislike = (LinkedList<String>) this.dislike.clone();
		temp.setIsActive(this.isActive);
		temp.title = this.title;
		return temp;
	}
	
	public void addIP(String ip){
		this.ip.add(ip);
	}
	
	/**
	 * Returns true if this IP has already voted
	 * @param ip
	 * @return
	 */	
	public boolean checkIP(String ip){
		if ((this.ip != null) && (!this.ip.isEmpty())) return this.ip.contains(ip);
		return false;
	}
	
	public void reset(){
		for (int i=0; i<optionResults.length; i++) optionResults[i]=0;
		like.clear();
		dislike.clear();
		ip.clear();
		numVotes=0;
	}
	
	public boolean getIsActive(){
		return isActive;
	}
	
	public void setIsActive(boolean isActive){
		this.isActive = isActive;
	}
	

}
